package com.miracle9.game.dao;

import com.miracle9.game.bean.Pager;
import com.miracle9.game.entity.Promoter;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class PromoterServiceDao extends SqlOperating<Promoter, Integer> {

    public void updatePayMoney(int userId, int payMoney) {
        q_CreateSql("update Promoter set payMoney = ? where id=?", new Object[]{Integer.valueOf(payMoney), Integer.valueOf(userId)}).executeUpdate();
    }

    public void updateExpiryNum(int userId, int gold) {
        q_CreateSql("update Promoter set expiryNum = ? where id=?", new Object[]{Integer.valueOf(gold), Integer.valueOf(userId)}).executeUpdate();
    }

    public Promoter getPromoterByUserName(String userName) {
        return (Promoter) q_CreateSqlFilter("from Promoter where username=?", new Object[]{userName});
    }

    public boolean updatePasswordById(int id, String password) {
        int cnt = q_CreateSql("update Promoter set password = ? where id=?", new Object[]{password, Integer.valueOf(id)}).executeUpdate();
        if (cnt > 0) {
            return true;
        }
        return false;
    }

    public Pager getPayApplyByUserIdAndInterval(int promoterId, String startTime, String endTime, Pager pager) {
        return q_CreateSqlsPager("from PayApply where userId = ? and datetime>=? and datetime<=? and userype=1 and type=0", pager, new Object[]{Integer.valueOf(promoterId), startTime, endTime});
    }

    public double getSumMoneyByUidAndDate(int promoterId, String date) {
        String hql = "select sum(money) from PayLog where userId = ? and datetime like ? and userType=1 and type=0";
        Object sum = q_CreateSql(hql, new Object[]{Integer.valueOf(promoterId), date + "%"}).uniqueResult();
        if (sum == null) {
            return 0.0D;
        }
        return ((Number) sum).doubleValue();
    }

    public double nestingGetSumMoneyBypromoterId(int promoterId, String userName, String date) {
        StringBuilder hql = new StringBuilder();
        hql.append("select sum(money) as revenue from ").append(" (select sum(money) as money from pay_log ").append(" where type=0 and parent_promoter_id = ? and LEFT(`datetime`,7) = ? ").append(" union all ").append(" select 0-sum(money) as money from pay_log ").append(" where type=1 and parent_promoter_id = ? and LEFT(`datetime`,7) = ? ").append(" union all ").append(" select sum(deposit) as money from computer_order ").append(" where admin = ? and LEFT(`borrow_date`,7) = ? ").append(" union all ").append(" select 0-sum(return_deposit) as money from computer_order ").append(" where status=1 and repay_admin = ? and LEFT(`repay_date`,7) = ? ").append(" ) t1");
        Object sum = s_CreateSql(hql.toString(), new Object[]{Integer.valueOf(promoterId), date, Integer.valueOf(promoterId), date, userName, date, userName, date}).uniqueResult();
        if (sum == null) {
            return 0.0D;
        }
        return ((Number) sum).doubleValue();
    }

    public Pager getPayApplyByUisAndInterval(int promoterId, String startTime, String endTime, Pager pager) {
        return q_CreateSqlsPager("from PayApply where userId = ? and datetime>=? and datetime<=? and userType=1 and type=1", pager, new Object[]{Integer.valueOf(promoterId), startTime, endTime});
    }

    public double getSumMoneyByUidAndInterval(int promoterId, String date) {
        String hql = "select sum(money) from PayLog where userId = ? and datetime like ? and userType=1 and type=1";
        Object sum = q_CreateSql(hql, new Object[]{Integer.valueOf(promoterId), date + "%"}).uniqueResult();
        if (sum == null) {
            return 0.0D;
        }
        return ((Number) sum).doubleValue();
    }

    public Pager ifGetPromoterParameter5(int promoterId, String userName, int pageNumber, int pageSize, Pager pager) {
        if (StringUtils.isNotBlank(userName)) {
            return q_CreateSqlsPager("from Promoter where parentId = ? and username like ?", pager, new Object[]{Integer.valueOf(promoterId), "%" + userName + "%"});
        }
        return q_CreateSqlsPager("from Promoter where parentId = ?", pager, new Object[]{Integer.valueOf(promoterId)});
    }

    public int ifGetPromoterParameter2(int promoterId, String searchWord) {
        if ((searchWord == null) || ("".equals(searchWord.trim()))) {
            String hql = "select sum(gold) from Promoter where parentId = ?";
            Object sum = q_CreateSql(hql, new Object[]{Integer.valueOf(promoterId)}).uniqueResult();
            if (sum == null) {
                return 0;
            }
            return ((Number) sum).intValue();
        }
        String hql = "select sum(gold) from Promoter where parentId = ? and username like ?";
        Object sum = q_CreateSql(hql, new Object[]{Integer.valueOf(promoterId), "%" + searchWord + "%"}).uniqueResult();
        if (sum == null) {
            return 0;
        }
        return ((Number) sum).intValue();
    }

    public int nestingGeSumCntByParentId(int promoterId) {
        String sql = "select sum(t.cnt) from (select count(*) as cnt from promoter where parent_id = ? and pay_money > 0  and state = 0 union all select count(*) as cnt from promoter where parent_id = ? and expiry_num > 0  and state = 0) t";
        Object sum = s_CreateSql(sql, new Object[]{Integer.valueOf(promoterId), Integer.valueOf(promoterId)}).uniqueResult();
        if (sum == null) {
            return 0;
        }
        return ((Number) sum).intValue();
    }

    public int getQuantityByparentIdAmdPaymoney(int promoterId) {
        String sql = "select count(*) from promoter where parent_id = ? and pay_money > 0";
        Object sum = s_CreateSql(sql, new Object[]{Integer.valueOf(promoterId)}).uniqueResult();
        if (sum == null) {
            return 0;
        }
        return ((Number) sum).intValue();
    }

    public int getQuantityByparentIdAmdExpiryNum(int promoterId) {
        String sql = "select count(*) from promoter where parent_id = ? and expiry_num > 0";
        Object sum = s_CreateSql(sql, new Object[]{Integer.valueOf(promoterId)}).uniqueResult();
        if (sum == null) {
            return 0;
        }
        return ((Number) sum).intValue();
    }

    public int nestingsGeSumCntByParentId(int promoterId) {
        String sql = "select sum(t.cnt) from (select count(*) as cnt from user where promoter_id = ? and pay_money > 0 and status in (0,1) union all select count(*) as cnt from user where promoter_id = ? and expiry_num > 0 and status in (0,1)) t";
        Object sum = s_CreateSql(sql, new Object[]{Integer.valueOf(promoterId), Integer.valueOf(promoterId)}).uniqueResult();
        if (sum == null) {
            return 0;
        }
        return ((Number) sum).intValue();
    }

    public int getQuantityByPromoterIdAndPayMoney(int promoterId) {
        String sql = "select count(*) from user where promoter_id = ? and pay_money > 0 and status in (0,1)";
        Object sum = s_CreateSql(sql, new Object[]{Integer.valueOf(promoterId)}).uniqueResult();
        if (sum == null) {
            return 0;
        }
        return ((Number) sum).intValue();
    }

    public int getQuantityByPromoterIdAndExpiryNum(int promoterId) {
        String sql = "select count(*) from user where promoter_id = ? and expiry_num > 0 and status in (0,1)";
        Object sum = s_CreateSql(sql, new Object[]{Integer.valueOf(promoterId)}).uniqueResult();
        if (sum == null) {
            return 0;
        }
        return ((Number) sum).intValue();
    }

    public boolean updateStateById_1(int promoterId) {
        int cnt = q_CreateSql("update Promoter set state = ? where id=?", new Object[]{Integer.valueOf(1), Integer.valueOf(promoterId)}).executeUpdate();
        if (cnt > 0) {
            return true;
        }
        return false;
    }

    public boolean updatePayMoney_0ById(int promoterId) {
        int cnt = q_CreateSql("update Promoter set payMoney = 0 where id=?", new Object[]{Integer.valueOf(promoterId)}).executeUpdate();
        if (cnt > 0) {
            return true;
        }
        return false;
    }

    public boolean updateExpiryNum_0ById(int promoterId) {
        int cnt = q_CreateSql("update Promoter set expiryNum = 0 where id=?", new Object[]{Integer.valueOf(promoterId)}).executeUpdate();
        if (cnt > 0) {
            return true;
        }
        return false;
    }

    public boolean getQuantityByUserId(int promoterId) {
        String hql = "select count(*) from PayApply where userId = ? and ammount>0 and UNIX_TIMESTAMP()*1000 < timeout and userType=1 and type=0 and state=0";
        Object sum = q_CreateSql(hql, new Object[]{Integer.valueOf(promoterId)}).uniqueResult();
        if (sum == null) {
            return true;
        }
        if (((Number) sum).intValue() > 0) {
            return false;
        }
        return true;
    }

    public boolean updateStateById_0(int promoterId) {
        int cnt = q_CreateSql("update Promoter set state = ? where id=?", new Object[]{Integer.valueOf(0), Integer.valueOf(promoterId)}).executeUpdate();
        if (cnt > 0) {
            return true;
        }
        return false;
    }

    public boolean updateRemarkById(int promoterId, String remark) {
        int cnt = q_CreateSql("update Promoter set remark = ? where id=?", new Object[]{remark, Integer.valueOf(promoterId)}).executeUpdate();
        if (cnt > 0) {
            return true;
        }
        return false;
    }

    public boolean addGold(int promoterId, int gold) {
        getSession().flush();
        getSession().clear();
        int cnt = q_CreateSql("update Promoter set gold = gold + ? where id=?", new Object[]{Integer.valueOf(gold), Integer.valueOf(promoterId)}).executeUpdate();
        if (cnt > 0) {
            return true;
        }
        return false;
    }

    public boolean updateChildrenPromoterNumById(int promoterId) {
        getSession().flush();
        getSession().clear();
        int cnt = q_CreateSql("update Promoter set childrenPromoterNum = childrenPromoterNum + 1 where id=?", new Object[]{Integer.valueOf(promoterId)}).executeUpdate();
        if (cnt > 0) {
            return true;
        }
        return false;
    }

    public boolean updateGoldById(int promoterId, int gold) {
        getSession().flush();
        getSession().clear();
        int cnt = q_CreateSql("update Promoter set gold = gold - ? where id=? and gold >= ?", new Object[]{Integer.valueOf(gold), Integer.valueOf(promoterId), Integer.valueOf(gold)}).executeUpdate();
        if (cnt > 0) {
            return true;
        }
        return false;
    }

    public boolean updateChildrenUserOverById(int id, int childOver) {
        Query query = q_CreateSql("update Promoter set childrenUserOver = ? where id=?", new Object[]{Integer.valueOf(childOver), Integer.valueOf(id)});
        int count = query.executeUpdate();
        return count > 0;
    }

    public boolean updateChildrenPayScaleId(int id, int payScale) {
        Query query = q_CreateSql("update Promoter set childrenPayScale = ? where id=?", new Object[]{Integer.valueOf(payScale), Integer.valueOf(id)});
        int count = query.executeUpdate();
        return count > 0;
    }

    public List<Promoter> getPromotersByPromoterId(int promoterId) {
        return q_CreateSqlList("from Promoter where parentId = ?", new Object[]{Integer.valueOf(promoterId)});
    }

}
